package com.qzq.weibo.fragment;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qzq.weibo.R;
import com.qzq.weibo.adapter.StatusAdapter;
import com.qzq.weibo.entity.Status;
import com.qzq.weibo.http.HttpRequest;
import com.qzq.weibo.view.LoadingDialog;
import com.qzq.weibo.view.PullRefreshAndLoadMoreListView;
import com.qzq.weibo.view.PullRefreshAndLoadMoreListView.OnLoadMoreListener;
import com.qzq.weibo.view.PullToRefreshListView.OnRefreshListener;

public class HomeTabFriendsFragment extends Fragment {

	private static HomeTabFriendsFragment mInstance;
	private View rootView;

	private LoadingDialog dialog_loading;
	private HttpRequest http;
	private StatusAdapter adapter;
	private PullRefreshAndLoadMoreListView lv_friend;
	private List<Status> statuses;

	private int page = 1;
	private int count = 5;

	public static HomeTabFriendsFragment getInstance() {

		if (mInstance == null) {
			mInstance = new HomeTabFriendsFragment();
		}
		return mInstance;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (rootView != null) {
			ViewGroup parent = (ViewGroup) rootView.getParent();
			if (parent != null) {
				parent.removeView(rootView);
			}
			return rootView;
		} else {
			View view = initView(inflater);

			initMember();
			loadingStatuses();

			rootView = view;
			return view;
		}
	}

	private View initView(LayoutInflater inflater) {
		View view = inflater.inflate(R.layout.fragment_home_friends, null);
		lv_friend = (PullRefreshAndLoadMoreListView) view
				.findViewById(R.id.home_friend_lv);

		return view;
	}

	private void initMember() {
		http = HttpRequest.getInstance(getActivity());
		statuses = new ArrayList<Status>();
		adapter = new StatusAdapter(getActivity(), R.layout.list_item_status,
				statuses);
		lv_friend.setAdapter(adapter);
		lv_friend.setOnRefreshListener(refreshListener);
		lv_friend.setOnLoadMoreListener(loadMoreListener);
	}

	/** 加载微博列表 */
	@SuppressLint("HandlerLeak")
	private void loadingStatuses() {
		dialog_loading = new LoadingDialog(getActivity(), R.style.Theme_Transparent,
				"登录中...");
		dialog_loading.show();
		page = 1;
		http.requestGetFriendsStatus(new Handler() {

			@SuppressWarnings("unchecked")
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				if (msg != null) {
					int flag = msg.what;
					lv_friend.onRefreshComplete();
					dialog_loading.dismiss();
					if (flag == HttpRequest.REQUEST_FAILER) {
						HttpRequest.badRequest(
								Integer.parseInt(msg.obj.toString()),
								getActivity());
					} else {
						statuses.clear();
						statuses.addAll((Collection<? extends Status>) msg.obj);
						adapter.notifyDataSetChanged();

					}
				}
			}
		}, page, count);
	}

	/** 下拉刷新 */
	private OnRefreshListener refreshListener = new OnRefreshListener() {

		@Override
		public void onRefresh() {
			loadingStatuses();
		}
	};

	/** 上拉加载更多 */
	private OnLoadMoreListener loadMoreListener = new OnLoadMoreListener() {

		@SuppressLint("HandlerLeak")
		@Override
		public void onLoadMore() {
			page += 1;
			http.requestGetHomeStatus(new Handler() {

				@SuppressWarnings("unchecked")
				@Override
				public void handleMessage(Message msg) {
					if (msg != null) {
						lv_friend.onLoadMoreComplete();
						int flag = msg.what;
						if (flag == HttpRequest.REQUEST_FAILER) {
							HttpRequest.badRequest(
									Integer.parseInt(msg.obj.toString()),
									getActivity());
						} else {
							statuses.addAll((Collection<? extends Status>) msg.obj);
						}
					}
					super.handleMessage(msg);
				}
			}, page, count);
		}
	};
}
